package lib

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/m"
	"fmt"

	log "github.com/sirupsen/logrus"
)

// 获取每日礼包信息
func GetDailyGift(u *m.User) (dg *m.DailyGift, err error) {
	dg = u.GetDailyGift()
	defer dg.Update(dg)

	now := utils.TNow()
	b := utils.IsSameDay(dg.CurTime, now.Unix())
	// 每日礼包当前时间 不等于 系统当前时间 加载当天的每日礼包活动
	if !b {
		// 获取每日礼包
		tpl, ok := data.Get("DailyPaidPacks", uint64(1))
		if !ok {
			err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到该配置表DailyPaidPacks")
			return
		}
		tplT := tpl.(data.DailyPaidPack)
		// 更新当天时间
		dg.CurTime = utils.ZeroTime(now)
		dg.BuyCnt = 0
		dg.Tid = int64(tplT.PackLst[0])
	}
	return
}

// 购买道具
func BuyDailyGift(from string, u *m.User, PSend func(interface{}) error) (buyingPackTid int64, dg *m.DailyGift, uProp *m.UpdateProp, resGoods []*m.Goods, boxResList []*m.BoxRes, err error) {
	dg = u.GetDailyGift()
	// 玩家购买付费礼包，走免费协议，不是超级账号，不得购买付费礼包。
	if from == "DailyGiftBuyRequest" && dg.BuyCnt > 0 && u.SuperAccount != 1 {
		err = errors.NewErrcode(data.Errcode.BuyFail, fmt.Sprintf("购买次数%d次，玩家 %d", dg.BuyCnt, u.SuperAccount))
		return
	}

	defer dg.Update(dg)

	// 获取每日礼包道具
	buyingPackTid = dg.Tid // 当前正在购买的每日礼包配表ID-在PaidPropsPack中的id
	tplT, err := GetPaidPropsPacksConfig(buyingPackTid)
	if err != nil {
		return
	}
	// 更换礼包道具，获取每日礼包
	tplD, ok := data.Get("DailyPaidPacks", uint64(1))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到该配置表DailyPaidPacks")
		return
	}
	tplDL := tplD.(data.DailyPaidPack)

	// 购买次数超过配表长度，不再购买
	var DailyPackLstCount int64 = int64(len(tplDL.PackLst))
	if dg.BuyCnt >= DailyPackLstCount {
		err = errors.NewErrcode(data.Errcode.BuyFail, fmt.Sprintf("购买次数超过%d次，id:%d", DailyPackLstCount, dg.BuyCnt))
		return
	}

	// 打包道具
	giveGoods := make([]m.BaseKV, 0)
	for _, item := range tplT.Goods {
		gtpl, ok := data.Get("Goods", item.Key)
		if !ok {
			err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到该配置表Goods")
			return
		}
		gtplT := gtpl.(data.Good)
		if int64(gtplT.Category) == data.GoodsCategoryEnum.CategoryLv3 { // 宝箱，需要一个一个给
			for i := int64(0); i < int64(item.Value); i++ {
				giveGoods = append(giveGoods, m.BaseKV{Key: int64(item.Key), Val: 1})
			}
		} else {
			giveGoods = append(giveGoods, m.BaseKV{Key: int64(item.Key), Val: int64(item.Value)})
		}
	}
	// 给用户道具
	uProp, resGoods, boxResList, err = GiveGoodsByKvList(from, u, giveGoods, PSend)
	if err != nil {
		return
	}

	// 增加购买次数
	dg.BuyCnt++
	if dg.BuyCnt < DailyPackLstCount {
		dg.Tid = int64(tplDL.PackLst[dg.BuyCnt])
	} else {
		// 购买三次后，没有道具
		dg.Tid = 0
	}
	return
}

// 重置每日礼包
func ResetDailyGift(u *m.User) {
	dg := u.GetDailyGift()
	defer dg.Update(dg)

	now := utils.TNow()

	// 获取每日礼包
	tpl, ok := data.Get("DailyPaidPacks", uint64(1))
	if !ok {
		log.WithFields(log.Fields{
			"uid":    u.Uid.Hex(),
			"method": "DailyGiftReqRequest",
		}).Debug("没有找到该配置表DailyPaidPacks")
		return
	}
	tplT := tpl.(data.DailyPaidPack)
	// 更新当天时间
	dg.CurTime = utils.ZeroTime(now)
	dg.BuyCnt = 0
	dg.Tid = int64(tplT.PackLst[0])
}
